<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 筛选功能 -->
      <el-card>
        <el-row> <!-- 行 -->
          <el-col :span="20"  ><!--列-->
            <el-form :inline="true" label-width="80px" size="small">
              <el-form-item label="关键字">
                <el-input style="200px" v-model="reqParams.keyword" placeholder="请输入关键字搜索"  ></el-input>

              </el-form-item>
              <el-form-item label="状态">
                <el-select placeholder="请选择" v-model="reqParams.state ">
                  <!--  -->
                  <el-option label="启用" :value="1"></el-option>
                  <el-option label="禁用" :value="0"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item style="text-align:right">
                <el-button @click="clear()">清除</el-button>
                 <!-- type="primary" 按钮样式  -->
                <el-button @click="filter()" type="primary" >搜索</el-button>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="4" style="text-align:right">
                <el-button @click="openDialog()" type="success" icon="el-icon-edit">新增技巧</el-button>
            </el-col>
        </el-row>
        <!-- 记录总数 -->
      <el-alert
      :title="`数据一共${total}条`"
       style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon>
     </el-alert>
     <!-- 表格 -->
      <el-table :data="articles" border >
    <!-- type列的类型，设置了 index 显示该行的索引（从 1 开始计算） -->
    <el-table-column label="序号" type="index" width="100px"></el-table-column>
    <el-table-column label="文章标题"   width="300px" >
    <template  plate slot-scope="scope">
      {{scope.row.title}}
      <!-- 阻止a的默认行为 -->
        <a @click.prevent="openVideo(scope.row.videoURL)" v-if="scope.row.videoURL" href="#" class="el-icon-film"></a>
    </template>
    </el-table-column>
    <el-table-column  label="阅读数" prop="visible"></el-table-column>
    <el-table-column  label="录入人" prop="username" ></el-table-column>
    <el-table-column  label="录入时间" >
          <template slot-scope="scope">{{scope.row.createTime|parseTimeByString}}</template>
    </el-table-column>
    <el-table-column  label="状态" width="100px">
          <template slot-scope="scope">{{scope.row.state===1?'已启用':'已禁用'}}</template>
    </el-table-column>
    <el-table-column  label="操作" width="200px">

          <template slot-scope="scope">
            <el-button type="text" @click="previewArticle(scope.row)">预览</el-button>
            <el-button type="text" @click="toggleState(scope.row)">{{scope.row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="openDialog(scope.row)" :disabled="scope.row.state===1">修改</el-button>
            <el-button type="text" @click="delArticle(scope.row)" :disabled="scope.row.state===1||scope.row.totals > 0">删除</el-button>
          </template>
        </el-table-column>
  </el-table>
  <!-- 分页 -->
     <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="reqParams.pagesize"
        :current-page="reqParams.page"
        @current-change="pager"
        :page-sizes="[5,10,20,50]"
        @size-change="handleSizeChange"
        hide-on-single-page
      ></el-pagination>
      </el-card>
      <!-- 添加|修改 -->
    <articles-add ref="articlesAdd" :data="currArticle"></articles-add>
 <!-- 预览 -->
    <articles-preview ref="articlesPreview" :data="currArticle"></articles-preview>
    <!-- 视频 -->
    <div class="video-preview" v-if="videoURL">
      <div class="close" @click="closeVideo()"><span class="el-icon-close"></span></div>
      <div class="box">
        <video ref="video" :src="videoURL"></video>
      </div>
<!--  -->
    </div>
  </div>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles.js'
import ArticlesAdd from '../components/articles-add.vue'
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  name: 'ArticlesList',
  components: { ArticlesAdd, ArticlesPreview },
  data () {
    return {
      reqParams: {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      articles: [],
      currArticle: {},
      videoURL: null
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 视频
    openVideo (url) {
      this.videoURL = url
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    closeVideo () {
      this.$refs.video.pause()
      this.videoURL = null
    },
    // 添加 编辑
    openDialog (article = {}) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 删除
    async delArticle (article) {
      await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(article)
      this.$message.success('删除成功')
      this.getList()
    },
    // 预览
    previewArticle (article) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesPreview.open()
      })
    },
    // 切换状态
    async toggleState (article) {
      await changeState({
        id: article.id,
        state: article.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      article.state = article.state === 1 ? 0 : 1
    },
    // 清除筛选条件
    clear () {
      this.reqParams = {
        keyword: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 进行筛选
    filter () {
      this.reqParams.page = 1
      this.getList()
    },
    // 查询列表
    async getList () {
      const { data } = await list(this.reqParams)
      this.articles = data.items
      this.total = data.counts
    },
    // 切换条数
    handleSizeChange (size) {
      this.reqParams.page = 1
      this.reqParams.pagesize = size
      this.getList()
    },
    // 切换分页
    pager (np) {
      this.reqParams.page = np
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currArticle.id) {
        this.reqParams.page = 1
      }
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard-container{
  padding: 10px;
  .el-icon-film{
    color: blue;
    font-size: 18px;
  }
}
.video-preview{
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.3);
  overflow: hidden;
  z-index: 9999;
  .close{
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%,0);
    background: rgba(0,0,0,0.4);
    box-shadow: 0 0 5px rgba(0,0,0,0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box{
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    video{
      width: 100%;
      height: 100%;
    }
  }
}
</style>
